package com.foreknow.dao.impl;

import com.foreknow.dao.AdminDao;
import com.foreknow.mapping.EntityMapping;
import com.foreknow.mapping.MappingFactory;
import com.foreknow.model.Admin;

import java.sql.SQLException;
import java.util.List;

public class AdminDaoImpl extends BaseDAO implements AdminDao {
    /**
     * 数据库查询方法
     * @param username
     * @param password
     * @return
     * @throws SQLException
     */
    @Override
    public Admin isCheckLogin(String username, String password) throws SQLException {
        String sql="select * from admin where username=? and password=?";
        EntityMapping mapping = this.mappingFactory.getMap(MappingFactory.ADMIN_MAPPING);
        //将从数据库查询到的值保存到集合
        List<Object> list = this.jdbcTemplate.query(sql, mapping, username, password);
        if (list.size()>0){
            return (Admin) list.get(0);
        }else{
            return null;
        }
    }

    /**
     * 查询所有用户名
     * @return
     */
    @Override
    public boolean selectNameAll(String username) {
        String sql="select * from admin where username=?";
        EntityMapping mapping=this.mappingFactory.getMap(MappingFactory.ADMIN_MAPPING);
        List<Object> list = this.jdbcTemplate.query(sql, mapping,username);
        if (list.size()>=1){
            return true;
        }else {
            return false;
        }
    }
}
